package org.jeecg.modules.yfRepair.Job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.kuawcsFs.entity.AgKuawcsFs;
import org.jeecg.modules.kuawcsFs.service.IAgKuawcsFsService;
import org.jeecg.modules.yfRepair.entity.HqYfRepair;
import org.jeecg.modules.yfRepair.entity.HqYfRepairPiece;
import org.jeecg.modules.yfRepair.service.IHqYfRepairPieceService;
import org.jeecg.modules.yfRepair.service.IHqYfRepairService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.*;
import java.util.Date;
import java.util.List;
@Slf4j
public class syncYfRepairPiece implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format("同步营房报修piece，任务执行了，时间:" + DateUtils.formatDateTime()));
        syncHqYfRepirePiece();
    }
    @Autowired
    private IHqYfRepairPieceService hqYfRepairPieceService;

    @Autowired
    private IAgKuawcsFsService agKuawcsFsService;


    @Value("${kwakcs.savePath}")
    String savePath ;
    /*
    同步数据
     */
//    @Scheduled(cron = "0 */1 * * * ?")
//    @GetMapping(value = "/tongbu")
    void syncHqYfRepirePiece(){
       // System.out.println("营房报修piece同步开始");

//读取数据
        List <HqYfRepairPiece> hqYfRepairPiece = hqYfRepairPieceService.list(new QueryWrapper <HqYfRepairPiece>().eq("chuanswczt","0"));

        if (!hqYfRepairPiece.isEmpty()){
            JSONArray jsonArray = new JSONArray();

            for (int i = 0; i < hqYfRepairPiece.size(); i++) {
                jsonArray.add(hqYfRepairPiece.get(i));
            }
            //写为文件
            BufferedWriter writer = null;
            String fileName="hq_yf_repair_piece"+".json";
            String path =savePath+"/数据同步/"+fileName;
            File file = new File(path);
            String parentPath = file.getParent();
            File parentFile = new File(parentPath);
            if(!parentFile.exists()) {
                parentFile.mkdirs();
            }
            //如果文件不存在，则新建一个
            if(!file.exists()){
                try {
                    file.createNewFile();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 格式化json字符串
            String jsonData= JSON.toJSONString(jsonArray);
            //写入
            try {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
                writer.write(jsonData);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(writer != null){
                        writer.close();
                        System.out.println("文件生成成功！");

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件生成失败！");
                }
            }

            hqYfRepairPiece.forEach(item -> {
                item.setChuanswczt("1");
                hqYfRepairPieceService.updateById(item);

            });


            //调用跨网传输
            AgKuawcsFs agKuawcsFs=new AgKuawcsFs();
            agKuawcsFs.setFasmbbm("houqin_erweima");
            agKuawcsFs.setFaswjm(fileName);
            agKuawcsFs.setFaswjlj(path);
            agKuawcsFs.setChuansfs("2");
            agKuawcsFs.setFassj(new Date());
          //  agKuawcsFsService.save(agKuawcsFs);
            agKuawcsFsService.sendFile(agKuawcsFs);
        }

    }
}
